package com.wc.AlgoOJ_train.train5_数据结构.c4_二叉树.DS0303_二叉树的最近公共祖先;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/3/3 10:25
 * @description http://43.138.190.70:8888/p/DS0303
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1010;
    // t[i][0] 表示是他的父节点，t[i][1]左孩子，t[i][2]右孩子
    static int[][] t = new int[N][3];
    static int n, u, v;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            t[i][1] = sc.nextInt();
            t[i][2] = sc.nextInt();
            if (t[i][1] != 0) t[t[i][1]][0] = i;
            if (t[i][2] != 0) t[t[i][2]][0] = i;
        }
        u = sc.nextInt();
        v = sc.nextInt();
        out.println(lca(u, v));
        out.flush();
    }


    // 上传递
    static int lca(int u, int v) {

        boolean[] exist = new boolean[N];
        while (u != 0) {
            exist[u] = true;
            u = t[u][0];
        }
        while (!exist[v]) {
            v = t[v][0];
        }
        return v;
    }
}


class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}